#!/bin/bash
clear
RED="\033[31m"    # Error message
GREEN="\033[32m"  # Success message
YELLOW="\033[33m" # Warning message
BLUE="\033[36m"   # Info message
RESET='\033[0m'

if [ "$EUID" -ne 0 ]; then
  echo -e "${RED} Anda tiada kebenaran untuk menjalankan skrip ini! ${RESET}"
  exit 1
fi

# ipAddr=$(wget -qO- ipv4.icanhazip.com)
hostAddr=$(cat /usr/local/.environment | grep 'DOMAIN' | cut -d '=' -f 2 | head -n 1)
emailAddr=$(cat /usr/local/.environment | grep 'EMAIL' | cut -d '=' -f 2)
userName=$(cat /usr/local/.environment | grep 'USERNAME' | cut -d '=' -f 2)
userPass=$(cat /usr/local/.environment | grep 'PASSWORD' | cut -d '=' -f 2)
clientUID="f7a9a565-80df-4c59-963a-f7d6d6735289"
portServ=$(shuf -i10000-65000 -n1)
certFile="/usr/local/etc/v2ray/fullchain.crt"
keyFile="/usr/local/etc/v2ray/private.key"

function install_v2ray {
  wget -q -O - https://raw.githubusercontent.com/v2fly/fhs-install-v2ray/master/install-release.sh | bash

  systemctl disable v2ray
  systemctl stop v2ray
}

function config_v2ray {
  mkdir -p /usr/local/etc/v2ray/clients/trojan
  mkdir -p /usr/local/etc/v2ray/clients/vless
  mkdir -p /usr/local/etc/v2ray/clients/vmess
  touch /usr/local/etc/v2ray/clients/.accounts

  # trojan-tcp-tls
  echo "{
  \"log\": {
    \"loglevel\": \"warning\"
  },
  \"inbounds\": [
    {
      \"port\": $portServ,
      \"protocol\": \"trojan\",
      \"settings\": {
        \"clients\": [
          {
            \"password\":\"$userPass\",
            \"email\": \"$emailAddr\"
          }
        ]
      },
      \"streamSettings\": {
        \"network\": \"tcp\",
        \"security\": \"tls\",
        \"tlsSettings\": {
          \"alpn\": [
            \"http/1.1\"
          ],
          \"certificates\": [
            {
              \"certificateFile\": \"$certFile\",
              \"keyFile\": \"$keyFile\"
            }
          ]
        }
      }
    }
  ],
  \"outbounds\": [
    {
      \"protocol\": \"freedom\"
    }
  ]
}" >/usr/local/etc/v2ray/trojan-tcp.json

  # vless-tcp-tls
  echo "{
  \"log\": {
    \"loglevel\": \"warning\"
  },
  \"inbounds\": [
    {
      \"listen\": \"0.0.0.0\",
      \"port\": $portServ,
      \"protocol\": \"vless\",
      \"settings\": {
        \"clients\": [
          {
            \"id\": \"$clientUID\",
            \"level\": 0,
            \"email\": \"$emailAddr\"
          }
        ],
        \"decryption\": \"none\",
        \"fallbacks\": [
          {
            \"dest\": 8001
          },
          {
            \"alpn\": \"h2\",
            \"dest\": 8002
          }
        ]
      },
      \"streamSettings\": {
        \"network\": \"tcp\",
        \"security\": \"tls\",
        \"tlsSettings\": {
          \"serverName\": \"$hostAddr\",
          \"alpn\": [
            \"h2\",
            \"http/1.1\"
          ],
          \"certificates\": [
            {
              \"certificateFile\": \"$certFile\",
              \"keyFile\": \"$keyFile\"
            }
          ]
        }
      }
    }
  ],
  \"outbounds\": [
    {
      \"protocol\": \"freedom\",
      \"tag\": \"direct\"
    }
  ]
}" >/usr/local/etc/v2ray/vless-tcp.json

  # vless-ws-tls
  echo "{
  \"log\": {
    \"loglevel\": \"warning\"
  },
  \"inbounds\": [
    {
      \"port\": $portServ,
      \"listen\": \"127.0.0.1\",
      \"protocol\": \"vless\",
      \"settings\": {
        \"clients\": [
          {
            \"id\": \"$clientUID\",
            \"level\": 0,
            \"email\": \"$emailAddr\"
          }
        ],
        \"decryption\": \"none\"
      },
      \"streamSettings\": {
        \"network\": \"ws\",
        \"security\": \"none\",
        \"wsSettings\": {
          \"acceptProxyProtocol\": true,
          \"path\": \"/websocket\"
        }
      }
    }
  ],
  \"outbounds\": [
    {
      \"protocol\": \"freedom\"
    }
  ]
}" >/usr/local/etc/v2ray/vless-ws.json

  # vmess-tcp-tls
  echo "{
  \"log\": {
    \"loglevel\": \"warning\"
  },
  \"routing\": {
    \"domainStrategy\": \"AsIs\",
    \"rules\": [
      {
        \"type\": \"field\",
        \"ip\": [
          \"geoip:private\"
        ],
        \"outboundTag\": \"block\"
      }
    ]
  },
  \"inbounds\": [
    {
      \"listen\": \"0.0.0.0\",
      \"port\": $portServ,
      \"protocol\": \"vmess\",
      \"settings\": {
        \"clients\": [
          {
            \"id\": \"$clientUID\"
          }
        ]
      },
      \"streamSettings\": {
        \"network\": \"tcp\",
        \"security\": \"tls\",
        \"tlsSettings\": {
          \"certificates\": [
            {
              \"certificateFile\": \"$certFile\",
              \"keyFile\": \"$keyFile\"
            }
          ]
        }
      }
    }
  ],
  \"outbounds\": [
    {
      \"protocol\": \"freedom\",
      \"tag\": \"direct\"
    },
    {
      \"protocol\": \"blackhole\",
      \"tag\": \"block\"
    }
  ]
}" >/usr/local/etc/v2ray/vmess-tcp.json

  # vmess-ws-tls
  echo "{
  \"log\": {
    \"loglevel\": \"warning\"
  },
  \"routing\": {
    \"domainStrategy\": \"AsIs\",
    \"rules\": [
      {
        \"type\": \"field\",
        \"ip\": [
          \"geoip:private\"
        ],
        \"outboundTag\": \"block\"
      }
    ]
  },
  \"inbounds\": [
    {
      \"listen\": \"0.0.0.0\",
      \"port\": $portServ,
      \"protocol\": \"vmess\",
      \"settings\": {
        \"clients\": [
          {
            \"id\": \"$clientUID\"
          }
        ]
      },
      \"streamSettings\": {
        \"network\": \"ws\",
        \"security\": \"tls\",
        \"tlsSettings\": {
          \"certificates\": [
            {
              \"certificateFile\": \"$certFile\",
              \"keyFile\": \"$keyFile\"
            }
          ]
        }
      }
    }
  ],
  \"outbounds\": [
    {
      \"protocol\": \"freedom\",
      \"tag\": \"direct\"
    },
    {
      \"protocol\": \"blackhole\",
      \"tag\": \"block\"
    }
  ]
}" >/usr/local/etc/v2ray/vmess-ws.json

  cp /var/www/$hostAddr/privkey.pem /usr/local/etc/v2ray/privkey.key
  cp /var/www/$hostAddr/fullchain.pem /usr/local/etc/v2ray/fullchain.crt
  chmod 644 /usr/local/etc/v2ray/fullchain.pem
  chmod 644 /usr/local/etc/v2ray/privkey.pem
}

function service_v2ray {
  systemctl daemon-reload

  systemctl enable v2ray@trojan-tcp
  systemctl start v2ray@trojan-tcp

  systemctl enable v2ray@vless-tcp
  systemctl start v2ray@vless-tcp
  systemctl enable v2ray@vless-ws
  systemctl start v2ray@vless-ws

  systemctl enable v2ray@vless
  systemctl start v2ray@vless
  systemctl enable v2ray@vmess
  systemctl start v2ray@vmess
}

function installation {
  install_v2ray
  config_v2ray
  service_v2ray

  echo
  echo -e "${GREEN} Pemasangan v2ray pakej telah selesai. ${RESET}"
  echo
}
